<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller 
{
	private $appId = 'wx3d22a018adfade7f';//打开微信公众平台-开发者中心 获取appId和appSecret
	private $appSecret = '1a1ab2892b77432d64cb96d03dac2fae';

    public function index()
    {
    	$back_url = WEB_HOST.'/index.php/home/index/get_unionid';
        $redirect_uri = urlencode($back_url);
    	$this->login($redirect_uri);;

    }


/*    public function get_openid(){
        $appid = "wx3d22a018adfade7f";
        $secret = "1a1ab2892b77432d64cb96d03dac2fae";
        $code = $_GET['code'];
        $state = $_GET['state'];
        $access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $token = json_decode(file_get_contents($access_token_url));
        //var_dump($token);
        $openid = $token->openid;
        print_r($openid);
    }*/
    public function login($redirect_uri) 
    {
		//微信授权登录
    	$appid = $this->appId; 
        $secret = $this->appSecret; 
        $state = 'dakang';
        $scope = 'snsapi_userinfo';
        // $redirect_uri = 'http://jsgx.ibenhong.com/index.php/home/index/get_unionid';
        $oauth_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
        header("Location: $oauth_url");
        //$this->redirect('index/main');
    }

  	public function get_unionid()
  	{
		//微信用户信息
    	//echo "123";exit;
        $appid = $this->appId; 
        $secret = $this->appSecret; 
        $code = $_GET['code'];
        $state = $_GET['state'];
        $access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $token = json_decode(file_get_contents($access_token_url));
        $openid = $token->openid;
        //print_r($openid);
		session('openid',$openid);
        //echo "<br>";
        $access_token = $token->access_token;
        //print_r($access_token);
        //header("Location: $access_token_url");
        $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $info = json_decode(file_get_contents($info_url));
        $data['wxname']   = $info->nickname;
        $data['picture_url'] = $info->headimgurl;
        $data['openid']     = $info->openid;
        $data['sex']        = $info->sex;
        $data['language']   = $info->language;
        $data['city']       = $info->city;
        $data['province']   = $info->province;
        $data['country']    = $info->country;
        $id = $data['openid'];
        if ( $id=='') {
        	$this->redirect('index/index'); 
        }
        session('openid',$id);
        $consumer=M('consumer');
        $res=$consumer->where("openid='$id'")->find();
        if ($res=='') {
        	$data['new_vip']="1";
        	$re=$consumer->data($data)->add();	
        }else{

        	$this->redirect('index/main'); 
        }
    }

	public function main()
	{
		vendor('Jssdk.Jssdk');
		$jssdk = new \Jssdk($this->appId, $this->appSecret);
		$wx = $jssdk->getSignPackage();	
		$host = 'http://'.$_SERVER['HTTP_HOST'];
    	$data = M('banner')->select();
    	$goods = M('goods')->select();
		$this->assign('wx',$wx);
    	$this->assign('openid',session('openid'));
    	$this->assign('host',$host);
    	$this->assign('goods',$goods);
    	$this->assign('data',$data);
    	$this->assign('flag','main');
        $this->display();
	}
	
	public function get_config()
	{
		vendor('Jssdk.Jssdk');
		$jssdk = new \Jssdk($this->appId, $this->appSecret);
		$wx = $jssdk->getSignPackage();	
		// echo json_encode($wx);
		return $wx;
	}

	public function product_det(){
		//$gid=15;
		$g_id = I('get.g_id');
		session('relet',I('get.relet'));
		session('g_id',$g_id);
		if(empty(session('openid'))) {
			$redirect_uri = WEB_HOST.'/index.php/home/index/product_details';
			$this->login($redirect_uri);
		} else {
			$this->redirect('index/product_details');
		}
	}
	public function product_details() 
	{
		if(empty(session('openid'))) {
			$appid = $this->appId; 
	        $secret = $this->appSecret; 
	        $code = $_GET['code'];
	        $state = $_GET['state'];
	        $access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
	        $token = json_decode(file_get_contents($access_token_url));
	        $openid = $token->openid;
	        //print_r($openid);
			session('openid',$openid);
		} else {
			$g_id = session('g_id');
			if(empty($g_id))
				$g_id=I('get.gid');
			session('g_id', null);
		}
		$re=M('goods')->where("id=$g_id")->find();
		$i=0;
		$data=M('g_price')->where("g_id=$g_id")->select();
		foreach ($data as $key => $value) {
			if ($value['stali']==1) {
				$data[$i]['stali']="(新用户)";
			} else {
				$data[$i]['stali']='';
			}
			$i++;
		}
		$p_t = array_chunk($data, 2);
		$host=WEB_HOST;
		$wx=$this->get_config();
		$this->assign('wx',$wx);
		$this->assign('openid',session('openid'));
		$this->assign('host',$host);
		$this->assign('price_time',$price_time);
		$this->assign('p_t',$p_t);
		$this->assign('re',$re);
        $this->display();
	}

	public function About_Us()
	{
		$banner=M('about_us');
    	$data=$banner->where('id=1')->find();
    	$this->assign('data',$data);
    	$this->assign('flag','about_us');
        $this->display();
	}

	public function personal_center()
	{
		$openid=session('openid');
		//print_r($openid);exit();
		$banner=M('consumer');
    	$data=$banner->where("openid='$openid'")->find();
    	//print_r($data);exit();
    	$this->assign('flag','personal');
    	$this->assign('data',$data);
        $this->display();
	}

	public function share_login($share_openid)
	{
		session('share_openid',$share_openid);
		$uri = WEB_HOST.'/index.php/home/index/share';
		$this->login($uri);
	}

    public function share()
    {
    	$appid = $this->appId; 
        $secret = $this->appSecret; 
        $code = $_GET['code'];
        $state = $_GET['state'];
        $access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $token = json_decode(file_get_contents($access_token_url));
    	$openid = $token->openid;
        $access_token = $token->access_token;
        $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $info = json_decode(file_get_contents($info_url));
        $consumer=M('consumer');
        $res=$consumer->where("openid='$openid'")->find();
        if (empty($res)) {
	        $info_data['wxname']   = $info->nickname;
	        $info_data['picture_url'] = $info->headimgurl;
	        $info_data['openid']     = $info->openid;
	        $info_data['sex']        = $info->sex;
	        $info_data['language']   = $info->language;
	        $info_data['city']       = $info->city;
	        $info_data['province']   = $info->province;
	        $info_data['country']    = $info->country;
        	$data['new_vip']="1";
        	$consumer->data($data)->add();	
        }
    	$share_openid = session('share_openid');
        $user_coupon_list = M('user_coupon');
        $list_data = $user_coupon_list->join('consumer ON consumer.openid = user_coupon.user_openid','LEFT')
        ->where('user_coupon.share_openid = "'.$share_openid.'"')->select();
        $coupon = M('coupon');
    	$coupon_data = $coupon->where('status = "N"')->find();
    	$coupon_id = $coupon_data['id'];
    	$coupon_name = $coupon_data['coupon_name'];
    	$randValue = rand(1,10);
    	$share = M('user_coupon');
    	$exist = $share->where("user_openid ='".$openid."' AND share_openid ='".$share_openid."'")->find();
    	if(!empty($exist)) {
    		$this->assign('randValue',$exist['value']);
    		// if($openid == $share_openid) 
    		// 	$this->assign('flag','2');
    		// else 
			$this->assign('flag','1');
    		$this->assign('list_data',$list_data);
    	} else {
	        $data['wxname']   = $info->nickname;
	        $data['picture_url'] = $info->headimgurl;
	    	$date = date('Y-m-d');
			// $consumer = M('consumer');
	    	// $data = $consumer->where("openid='$openid'")->find();
	    	// $this->assign('data',$data);
	    	$this->assign('randValue',$randValue);
	    	$this->assign('value',$value);
	    	$user_coupon = M('user_coupon');
	    	$user_coupon->data(array(
	    		'user_openid' => $openid,
	    		'share_openid' => $share_openid,
	    		'coupon_id' => $coupon_id,
	    		'coupon_name' => $coupon_name,
	    		'value' => $randValue,
	    		'date' => date('Y-m-d h:i:s')
			))->add();
			$list_data = array('0' => array(
				'picture_url' => $data['picture_url'],
				'wxname' => $data['wxname'],
				'date' => $date,
				'value' => $randValue
			));
			$this->assign('list_data',$list_data);
    	}
    	// $this->assign('data',$data); 
    	$this->display();
    }

/*	public function personal_data()
	{
		$openid=session('openid');
		$banner=M('consumer');
    	$data=$banner->where("openid='$openid'")->find();
    	
     	if($data['sex']==1){
                $data['sex']="";
        }
        else{
                $data['sex']="selected";
        }
    	$this->assign('data',$data);
        $this->display();
	}*/

	public function search_product() 
	{
		$data = $_REQUEST['content'];
		$product = M('goods');
		$id = $product->where('name like "%'.$data.'%"')->field('id')->find();
		if(empty($id)) 
			echo '';
		else 
			echo json_encode($id);
	}

 	public function personal_data()
	{
		$openid=session('openid');
		//$openid = 'oPL7sv1468vnCsIIpIW__ZytoqvA';
		$banner=M('consumer');
    	$data=$banner->where("openid='$openid'")->find();
    	$this->assign('data',$data);
        $this->display();
	}

	public function upload_pic()
	{
		$openid=session('openid');
		$data = ['picture_url' => $this->upload()];
		$upl=M('consumer');
		$re=$upl->where("openid='$openid'")->save($data); 
	}

	public function upload() 
	{
		$upload = new \Think\Upload();// 实例化上传类
	   	// $upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
		// 上传单个文件 
		$key = array_keys($_FILES)[0];
		$info   =   $upload->uploadOne($_FILES[$key]);
		if(!$info) {// 上传错误提示错误信息
		   $this->error($upload->getError());
		}else{// 上传成功 获取上传文件信息
		    echo $info['savepath'].$info['savename'];
		    return '/Uploads/'.$info['savepath'].$info['savename'];
		}
	}

    public function personal_coupon()
    {
    	$openid = session('openid');
    	$coupon = M('user_coupon');
    	$data = $coupon->where("user_openid='{$openid}'")->select();
    	$host = $_SERVER['HTTP_HOST'];
    	$this->assign('data',$data);
    	$this->assign('host',$host);
		$this->assign('flag',I('get.flag'));
    	$this->display();
    }

	public function xg_consumer()
	{
		$data = I('post.');
		$openid=session('openid');
		$consumer=M('consumer')->where("openid='$openid'")->save($data);
		if ($consumer=='') {
			$this->redirect('index/personal_center'); 
		}else{
			$this->redirect('index/personal_data'); 
		}
	}

	public function autonym()
	{
		$openid=session('openid');
		$data=I('post.');
		$banner=M('consumer');
		$re=$banner->where("openid='$openid'")->save($data); 
		if ( false == $result ){   
        	$this->success('验证成功！','./personal_data',0);  
        }else{   
        	$this->error('删除失败！');     
        }
		
	}
	public function The_user_id()
	{
		$openid=session('openid');
		$banner=M('consumer');
		$re=$banner->where("openid='$openid'")->find(); 
		$this->assign('re',$re);
		$this->display();
	/*	if ($re['id_number']=='') {

			$this->redirect('./The_user_id', 0, '页面跳转中...');
		} else {
			$this->assign('data',$re);
			$this->redirect('./The_user_id_1', 0, '页面跳转中...');
		}*/
		
	}
	public function ajax_price (){
		$g_id=$_REQUEST["product"];
		$data=M('g_price')->where("g_id=$g_id")->select();
		//print_r($data);
		$i = 0;
		foreach ($data as $key => $value) {
			if ($value['stali']==1) {
				$value['stali']="(新用户)";
			} else {
				$value['stali']='';
			}
			if($i == 0) {
				echo "
					<div class='col-md-6 col-sm-6 col-xs-6'>
						<p class='text-left small-text'>
							<input type='radio' checked name='price'>
								".$value['price']."元 (".$value['time']."天)".$value['stali']."				
						</p>	
					</div>
				";
			} else {
				echo "
					<div class='col-md-6 col-sm-6 col-xs-6'>
						<p class='text-left small-text'>
							<input type='radio' name='price'>
								".$value['price']."元 (".$value['time']."天)".$value['stali']."				
						</p>	
					</div>
				";	
			}
			$i++;
		}
		
	}

	public function update_default() 
	{
		$data['id'] = $_REQUEST['id'];
		$openid = session('openid');
		$address = M('address');
		if($_REQUEST['confirm'] == 'true') {
			$data['state'] = '0';
			$data2['state'] = '1';
			$address->where('id = '.$data['id'])->save($data);
			$address->where('id != '.$data['id'].' AND openid = "'.$openid.'"')->save($data2);
		}
		session('addr_id',$data['id']);
		// $addr = $address->where('id = '.$data['id'])->find();
		// $this->assign('data',$addr);
	}

	public function address_administration() 
	{
		$openid = session('openid');
		$flag = I('get.flag');
		if($flag == 'submit_order') {
			$this->assign('choose_addr','true');
		}
		$data = M('address')->where("openid='$openid'")->select();
		if ($data=='') {
			$display = "";
		}else{
			foreach ($data as $key => $value) {
				if ($value['state'] == '0') {
					$data[$key]['states'] = "<p class='tl pa highlight'>[默认]</p>";
				}
			}
		}
		// print_r($data);
		$this->assign('data',$data);
		$this->display();
	}

	public function add() 
	{
		$addr_id = session('addr_id');
		if(empty($addr_id)) {
			$this->display();
		} else {
			$addr = M('address');
			$data = $addr->where('id = "'.$addr_id.'"')->field('id,name,tel,addr')->find();
			$this->assign('data',array('0' => $data));
			session('addr_id',null);
			$this->display();
		}
	}
	
	public function add_addr(){
		$data=I('post.');
		$openid=session('openid');
		$data['openid']=$openid;
		$g_id = session('g_id');
		//print_r($data);exit();
        $re = M('address');
        if(isset($data['id'])) {
        	$re->where('id = '.$data['id'])->save($data);
        } else {
			$se=M('address')->where("openid='$openid'")->select();
			if (!empty($se)) {
				$data['state'] = 1;
			}else{
				$data['state'] = 0;
			}
			if(!empty($data['name']) && !empty($data['tel']) && !empty($data['addr']))
        		$re->data($data)->add();
        }
        if ($re=='') {
        	echo "<script>window.location.href='./add_addr';</script>";
        }else{
        	if(empty($g_id)) {
        	 	echo "<script>window.location.href='./personal_center';</script>";
        	} else {
        		echo "<script>window.location.href='./submit_order';</script>";
        	}
        }
	}

	public function del_addr() {
		$openid=session('openid');
		$id=I('post.id');
		$re=M('address')->where("id='$id'")->delete();
	}

  	public function submit_order() {
  		$openid = session('openid');
        $price_id = I('post.price');
        $g_id = I('post.g_id');
        $coupon_id = I('get.coupon_id');
        $relet = session('relet');
        if($relet) {
        	$order_state = M('order_state')->where('order_state.order_id = "'.$relet.'"')->find();
        	$this->assign('order_state', $order_state);
        	$this->assign('relet', 'true');
        	session('relet',null);
        }
        if(empty($g_id)) {
        	$g_id = session('g_id');
        } else {
        	session('g_id',$g_id);
        }
        if(empty($price_id)) {
        	$price_id = session('price_id');
        } else {
        	session('price_id',$price_id);
        }
        $goods = M('goods')->where("id=$g_id")->find();
        $g_price = M('g_price')->where('id = '.$price_id)->find();
        if(empty($coupon_id)) {
        	$coupon = '';
        } else {
        	$coupon = M('user_coupon')->where('id = '.$coupon_id)->find();
        }
        $goods['xiaoji'] = $g_price['price'] - $goods['ps_price'];
        $re = M('consumer')->where("id='$openid'")->find();
        if(!empty(I('get.addr_id'))) {
        	$addr_id = I('get.addr_id');
	        $addr = M('address')->where("id=$addr_id")->find();
        } else {
	        $addr = M('address')->where("openid='$openid' AND state = '0'")->find();
        }
        //$order['order_id']=time();
        //$order['order_id']=time();
        $map['price'] = $price_id;
		$map['_string'] = "g_id='$g_id'";
		$g_id = M('g_price')->where($map)->find();
		$goods['g_id'] = $g_id['id'];
		$this->assign('coupon', $coupon);
		$this->assign('g_price', $g_price);
        $this->assign('addr', $addr);
        $this->assign('goods', $goods);
        $this->display();
    }

 	public function pay_ok()
 	{
 		$openid=session('openid');
 		//print_r($openid);exit();
 		$order=M('order');
 		$re=$order->where("user='$openid'")->order(array('or_time'=>'desc'))->limit(1)->select(); 
 		//print_r($re);
 		$da['state']=1;
 		$data=$order->where("user='$openid'")->save($da);
 	}

 	public function all_orders($value='')
 	{
 		$openid=session('openid');
 		//print_r($openid);exit();
 		$order=M('order');
 		if(empty($_REQUEST['product']) || $_REQUEST['product'] == 'all') {
 			$where['state'] = array('EGT',0);
 		} else {
 			$where['state'] = array('EQ',$_REQUEST['product']);
 		}
		$where['_string'] = "user='$openid'";
 		$data=$order->where($where)->select();
 		$goods=M('goods');
 		$g_price=M('g_price');
 		foreach ($data as $key => $value) {
			if ($value['state']==0) {
				$data[$key]['img']="sqqx.png";
			}
			if ($value['state']==1) {
				$data[$key]['img']="psing.png";
			}
			if ($value['state']==2) {
				$data[$key]['img']="yiwancheng.png";
			}
			if ($value['state']==3) {
				$data[$key]['img']="yiwancheng.png";
			}
			$g_id=$value['goods_id'];
			$g_price_id=$value['g_price_id'];
			$re1=$goods->where("id=".$g_id)->find();
			$re2=$g_price->where("id=".$g_price_id)->find();
			$data[$key]['abstract']=$re1['abstract'];
			$data[$key]['day']=$re2['time'];
		}
 		//print_r($data);exit();
 		$this->assign('data',$data);
 		$this->display();
 	}

 	public function ajax_orders(){
 		//echo"123";
		$openid=session('openid');
 		//print_r($openid);exit();
 		$order=M('order');
 		$state=$_REQUEST["product"];
 		$goods=M('goods');
 		$g_price=M('g_price');
		switch($state){ 
				case 'all': 
				//未配送
				$where['state'] = array('EGT',0);
				$where['_string'] = "user='$openid'";
				$data=$order->where($where)->select();
				foreach ($data as $key => $value) {
					if ($value['state']==0) {
						$value['state']="<img src='/public/home/img/sqqx.png'>";
					}
					if ($value['state']==1) {
						$value['state']="<img src='/public/home/img/psing.png'>";
					}
					if ($value['state']==2) {
						$value['state']="<img src='/public/home/img/yiwancheng.png'>";
					}
					if ($value['state']==3) {
						$value['state']="<img src='/public/home/img/yiwancheng.png'>";
					}
					$g_id=$value['goods_id'];
					$re=$goods->where("id=$g_id")->find();
					$g_price_id=$value['g_price_id'];
					$re1=$g_price->where("id=$g_price_id")->find();
					echo "<div class='trik'></div>
						<div class='indent cancel' onclick="
						."window.location.href='/index.php/home/index/cancellation_of_order.html?o_id="
						.$value['id']."'"
						.">
							<div class='indent-top'>
								<p class='fl'>订单ID: ".$value['order_id']."</p>
								<p class='fr'>
									".$value['state']."
								</p>
							</div>
							<div class='long-line'></div>
							<div class='indent-bottom'>
								<p class='tl fl'>".$re['abstract']."</p>
								<p class='gray'>".$re1['time']."天套餐</p>
								<p class='text gray'><span class='fl'>".$value['or_time']."</span><span class='fr'>1份</span></p>
							</div>
						</div>";
		 		}
				break; 
				case 0: 
				//未配送
				$where['state'] = array('eq',0);
				$where['_string'] = "user='$openid'";
				$data=$order->where($where)->select();
				foreach ($data as $key => $value) {
					$g_id=$value['goods_id'];
					$re=$goods->where("id=$g_id")->find();
					$g_price_id=$value['g_price_id'];
					$re1=$g_price->where("id=$g_price_id")->find();
					echo "<div class='trik'></div>
						<div class='indent cancel' onclick="
						."window.location.href='/index.php/home/index/cancellation_of_order.html?o_id="
						.$value['id']."'"
						.">
							<div class='indent-top'>
								<p class='fl'>订单ID: ".$value['order_id']."</p>
								<p class='fr'>
									<img src='/public/home/img/sqqx.png'>
								</p>
							</div>
							<div class='long-line'></div>
							<div class='indent-bottom'>
								<p class='tl fl'>".$re['abstract']."</p>
								<p class='gray'>".$re1['time']."天套餐</p>
								<p class='text gray'><span class='fl'>".$value['or_time']."</span><span class='fr'>1份</span></p>
							</div>
						</div>";
		 		}
				break; 
				case 1: 
					//配送中
				$where['state'] = array('eq',1);
				$where['_string'] = "user='$openid'";
				$data=$order->where($where)->select();
				foreach ($data as $key => $value) {
					$g_id=$value['goods_id'];
					$re=$goods->where("id=$g_id")->find();
					$g_price_id=$value['g_price_id'];
					$re1=$g_price->where("id=$g_price_id")->find();
					echo "<div class='trik'></div>
						<div class='indent cancel' onclick="
						."window.location.href='/index.php/home/index/cancellation_of_order.html?o_id="
						.$value['id']."'"
						.">
							<div class='indent-top'>
								<p class='fl'>订单ID: ".$value['order_id']."</p>
								<p class='fr'>
									<img src='/public/home/img/psing.png'>
								</p>
							</div>
							<div class='long-line'></div>
							<div class='indent-bottom'>
								<p class='tl fl'>".$re['abstract']."</p>
								<p class='gray'>".$re1['time']."天套餐</p>
								<p class='text gray'><span class='fl'>".$value['or_time']."</span><span class='fr'>1份</span></p>
							</div>
						</div>";
		 		}
				break; 
				case 2: 
					//完成配送
				$where['state'] = array('EGT',2);
				$where['_string'] = "user='$openid'";
				$data=$order->where($where)->select();
				foreach ($data as $key => $value) {
					$g_price_id=$value['g_price_id'];
					$re1=$g_price->where("id=$g_price_id")->find();
					echo "<div class='trik'></div>
						<div class='indent cancel' onclick="
						."window.location.href='/index.php/home/index/cancellation_of_order.html?o_id="
						.$value['id']."'"
						.">
							<div class='indent-top'>
								<p class='fl'>订单ID: "
								.$value['order_id']."</p>
								<p class='fr'>
									<img src='/public/home/img/yiwancheng.png'>
								</p>
							</div>
							<div class='long-line'></div>
							<div class='indent-bottom'>
								<p class='tl fl'>".$re['abstract']."</p>
								<p class='gray'>".$re1['time']."天套餐</p>
								<p class='text gray'><span class='fl'>".$value['or_time']."</span><span class='fr'>1份</span></p>
							</div>
						</div>";
		 		}
				break; 
		}
 	}

    public function cancel_order() 
    {
    	$order_id = I('get.order_id');
    	M('order')->where("order_id='$order_id'")->delete();
    	$this->redirect('index/all_orders');
    }

 	public function cancellation_of_order($value='')
 	{
 		vendor('Jssdk.Jssdk');
		$jssdk = new \Jssdk($this->appId, $this->appSecret);
		$wx = $jssdk->getSignPackage();
		$openid = session('openid');	
		$host = 'http://'.$_SERVER['HTTP_HOST'];
 		$order_id = I('get.o_id');
 		$order = M('order')->field('order.goods_id,order.addr_id,order.g_price_id,order.state,order.price,order.order_id,order.or_time,goods.name,goods.headline,goods.conf_infor,goods.abstract,os.freight,os.xd_time')
 		->join('goods ON goods.id = order.goods_id','LEFT')
 		->join('order_state as os ON os.order_id = order.id','LEFT')->where("order.id=$order_id")->find();
 		$addr_id=$order['addr_id'];
 		$addr_db = M('address');
 		$addr = $addr_db->where("id=$addr_id")->find();
 		if(empty($address)) {
 			$addr = $addr_db->where('openid = "'.$openid.'" AND state = 0')->find();
 		}
 		$goods_id=$order['goods_id'];
 		$g_price_id = $order['g_price_id'];
 		$g_price = M('g_price')->where("id=$g_price_id")->find();
		$state = $order['state'];
		switch ($state){
			case 0:
			 	$show['title'] = "未配送";
			  break;  
			case 1:
				$show['title'] = "配送中";
			  break;
			case 2:
				$show['title'] = "已完成配送";
			  break;
			case 3:
				$show['title'] = "已完成配送";
			  break;
		}
		$show['state'] = $state;
		$this->assign('wx',$wx);
		$this->assign('host',$host);
		$this->assign('show',$show);	
		$this->assign('addr',$addr);
		$this->assign('order',$order);
		$this->assign('goods_id',$goods_id);
		$this->assign('g_price',$g_price);
 		$this->display();
 	}

}